package com.lzb.compressmaster.activity;

import android.os.Handler;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.adapter.PicSucAdapter;
import com.lzb.compressmaster.basic.utils.FileP2pUtil;
import com.lzb.compressmaster.databinding.ActivityPictureCompressSucBinding;
import com.lzb.compressmaster.utils.ImageCompressor;
import com.lzb.compressmaster.utils.RxUtil;

import java.util.List;

import io.reactivex.rxjava3.core.ObservableEmitter;

public class PictureCompressSucActivity extends BaseAc<ActivityPictureCompressSucBinding> implements View.OnClickListener {

    public static List<ImageCompressor.CompressResult> listResult;
    private PicSucAdapter picSucAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_picture_compress_suc;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPictureCompressSucBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        picSucAdapter = new PicSucAdapter();
        mDataBinding.rvPictureList.setAdapter(picSucAdapter);
        picSucAdapter.setList(listResult);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvSave) {//保存相册
            saveImg();
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvNum.setText(getString(R.string.total_picture_num, listResult.size()));
        int progress = (int) ((float) listResult.get(0).getCompressedSize() / listResult.get(0).getOriginalSize() * 100);
        mDataBinding.tvOriginalSize.setText(ConvertUtils.byte2FitMemorySize(listResult.get(0).getOriginalSize(), 2));
        mDataBinding.tvCompressedSize.setText(ConvertUtils.byte2FitMemorySize(listResult.get(0).getCompressedSize(), 2));
        mDataBinding.pbProgress.setProgress(progress);
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        for (ImageCompressor.CompressResult compressResult : listResult) {
                            FileP2pUtil.copyPrivateImgToPublic(mContext, compressResult.getOutputPath());
                        }
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        finish();
                    }
                });
            }
        }, 1000);
    }

}